<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class users extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model(array(
            'login_model',
            'user_model',
            ));
        $this->output->enable_profiler(TRUE);
    }
    
    public function index($offset = 0)
    {
        $userList = $this->user_model->userList($offset);
        $breakCrum = '<a href="' . base_url() . '">Admin</a> » <span>User</span>';
        $content = $this->load->view('users/main', $userList, true);
        $this->load->view('templates/admin', array(
            'content' => $content, 
            'breakCrum' => $breakCrum));
    }
    
    public function add()
    {
        if ($_POST) {
            if (!$this->user_model->checkFormAdd()) {
                notify_set_messages(validation_errors(), 'users');
                return $this->add();
            }
            $params['username'] = $this->input->post('username');
            $params['firtname'] = $this->input->post('firtname');
            $params['lastname'] = $this->input->post('lastname');
            $params['pass'] = imd5(trim($this->input->post('password')));
            $params['status'] = $this->input->post('status');
            $params['role'] = $this->input->post('role');
            
            $params['created'] = time();
            if ($this->user_model->doAdd($params)) {
                notify_set_messages(notify_info(t('User have been added')), 'users');
                return redirect(site_url("users"));
            }
        }
        $data = array("content" => $this->load->view("users/add", '', true));
        $this->load->view("templates/admin", $data);
    }
    
    public function edit($id = 0)
    {
        if ($_POST) {
            $params = $this->input->post();
           
            if (trim($params['password']) != '') {
                if (!$this->user_model->checkFormPass()) {
                    notify_set_messages(notify_error(validation_errors()),  'user');
                    redirect(site_url('users/edit/'.decrypt($params['idx'])));
                } else {
                    $this->user_model->changepass($params);
                }  
            }
            $this->user_model->updateInfo($params);
            $this->user_model->deleteRole(decrypt($params['idx']));
            $this->user_model->addRole(decrypt($params['idx']), $params['role']);
            redirect(site_url('users'));
        }
        $result = $this->user_model->GetUserById($id);
        if (empty($result)) {
            redirect(site_url('users'));
        }
       
        if ($id != $this->auth_model->userid() && !in_array(3, $this->auth_model->rid())) {
            redirect(site_url('users'));
        }
        $breakCrum = '<a href="' . base_url() . '">Admin</a> » <a href="' . site_url('users') .
                '">Users</a> » <span>Update</span>';
        $content = $this->load->view('users/edit', array('result' => $result), true);
        $this->load->view('templates/admin', array('content' => $content, 'breakCrum' =>
                    $breakCrum));
    }
    
    public function delete($id)
    {
        $this->user_model->deleteUser($id);
        redirect(site_url('users'));
    }

    public function update()
    {
        if (!$_POST) {
            return redirect(site_url('users'));
        }
        $params['firtname'] = $this->input->post('firtname');
        $params['lastname'] = $this->input->post('lastname');
        $params['status'] = $this->input->post('status');

        if (!$this->user_model->checkForm()) {
            notify_set_messages(validation_errors(), 'users');
            return $this->edit(decrypt($_POST["idx"]));
        }
        if ($this->user_model->doUpdate($params)) {
            notify_set_messages(notify_info(t('User have been updated!')), 'users');
            redirect(site_url("users"));
        }
    }
    
    public function changepass()
    {
        if ($_POST) {
            if (!$this->user_model->checkFormPass()) {
                notify_set_messages(validation_errors(), 'users');
                redirect(site_url("users/changepass"));
            }
            if ($this->user_model->doChangePass()) {
                notify_set_messages(notify_success(t('Password have been changed!')), 'users');
                redirect(site_url("users"));
            } 
        }
        $breakCrum = '<a href="' . base_url() . '">Admin</a> » <a href="' . site_url('users') .
            '">Users</a> » <span>Change password</span>';
        $userInfo = $this->user_model->GetUserById($this->auth_model->userid());
        $content = $this->load->view('users/changepass', array('result' => $userInfo), true);
        $this->load->view('templates/admin', array('content' => $content, 'breakCrum' =>
                $breakCrum));
    }

}
